package com.qinghe.pro.controller.front;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.qinghe.pro.entity.article.Article;
import com.qinghe.pro.entity.article.Column;
import com.qinghe.pro.service.article.ArticleService;
import com.qinghe.pro.service.article.ColumnService;
import com.qinghe.utils.Page;

@Controller
@RequestMapping("/article")
public class ArticlesController {

	@Autowired
	@Qualifier("articleServiceImpl")
	private ArticleService articleService;

	@Autowired
	@Qualifier("columnServiceImpl")
	private ColumnService columnService;

	private static final String VIEW = "article/view";
	private static final String LIST = "article/list";

	/**
	 * 查看新闻详情+右侧栏目显示
	 * 
	 * @param id
	 * @param map
	 */
	@RequestMapping(value = "/view/{id}", method = RequestMethod.GET)
	public String view(@PathVariable Integer id, Map<String, Object> map) {
		Article article = articleService.get(id);
		
		map.put("article", article);
		return VIEW;
	}

	/**
	 * 显示新闻列表
	 * 
	 * @param columnId
	 * @param page
	 * @param map
	 */
	@RequestMapping(value = "/list/{columnId}", method = { RequestMethod.GET, RequestMethod.POST })
	public String list(@PathVariable Integer columnId, Page page, Map<String, Object> map) {
		page.setNumPerPage(4);
		Article article = new Article();
		if (columnId != null)
			article.setColumn(new Column(columnId));
		List<Article> articles = articleService.findByPage(article, page);

		map.put("columnId", columnId);
		map.put("page", page);
		map.put("articles", articles);

		return LIST;
	}

}
